import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:mybase/base/super/FFGestureDetector.dart';
import 'package:mybase/base/super/FFInk.dart';
import 'package:mybase/base/tool/arouter/FFGoto.dart';
import 'package:mybase/base/tool/color/FFColor.dart';
import 'package:mybase/base/tool/empty/FFEmpty.dart';
import 'package:mybase/base/widget/layout/FFBranch.dart';
import 'package:mybase/base/widget/layout/FFImageButton.dart';
import 'package:mybase/base/widget/layout/FFLineContainer.dart';
import 'package:mybase/base/widget/layout/FFNavi.dart';
import 'package:mybase/base/widget/layout/FFTextButton.dart';

import '../../main.dart';

//基于FF 插件
class AppNavi {
  static String naviBackImg = "assets/images/navi/icon_nav_back@3x.png";
  static EdgeInsetsGeometry padding = const EdgeInsets.all(FFNaviHeight * 0.28);

  ///导航条
  static createNavi(
    BuildContext context, {
    Widget? leading,
    String? leadCustomImg, //左侧自定义返回icon
    String? title, //标题文字
    bool? titleCenter = false, //标题是否居中
    bool? leadingHidden = false, // 返回按扭是否隐藏
    Widget? actionView, //右侧自定义widget, 比actions优先级高
    List<DueNaviAction>? actions, //右侧按扭
  }) {
    List<Widget>? children;
    if (actionView == null) {
      if (!FFEmpty.listIsEmpty(actions)) {
        children = [];
        for (int i = 0; i < actions!.length; i++) {
          DueNaviAction element = actions[i];
          if (element.type == DueNaviActionType.img) {
            children.add(
              DueNaviExt.naviBtn(context,
                  image: element.value ?? "",
                  disabled: !(element.canClick!), onClick: () {
                if (element.onClick != null) element.onClick!();
              }),
            );
          } else if (element.type == DueNaviActionType.text) {
            children.add(
              FFInk(
                disabled: !(element.canClick!),
                child: FFTextButton(
                  width: FFNaviHeight,
                  heigth: FFNaviHeight,
                  alignment: Alignment.center,
                  text: element.value,
                  fontSize: 14,
                  fontColor: element.fontColor,
                  fontWeight: FontWeight.w500,
                  onClick: () {
                    if (element.onClick != null) element.onClick!();
                  },
                ),
              ),
            );
          } else {
            // custom
            if (element.customWidget != null) {
              children.add(FFInk(child: element.customWidget!,disabled: !(element.canClick!)));
            }
          }
        }
      }

      actionView = FFLineContainer(
        direction: Axis.horizontal,
        children: children,
      );
    }

    return FFNavi(
      color: Colors.transparent,
      height: FFNaviHeight,
      insetHorizontal: 8,
      leading: leading ??
          FFLineContainer(
            direction: Axis.horizontal,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              FFBranch(
                condition: leadingHidden == false,
                trueWidget: DueNaviExt.backBtn(context, image: leadCustomImg),
              ),
              FFBranch(
                condition: titleCenter == false,
                trueWidget: Container(
                  width:
                      MediaQuery.of(navigatorKey.currentContext!).size.width *
                          (FFEmpty.listIsEmpty(children) == true ? 0.75 : 0.5),
                  child: DueNaviExt.titleWidget(context, title: title),
                ),
              ),
            ],
          ),
      titleView: FFBranch(
        condition: titleCenter == true,
        trueWidget: DueNaviExt.titleWidget(context, title: title),
      ),
      actionView: actionView,
    );
  }

}

extension DueNaviExt on AppNavi {

  ///导航栏 按扭
  static Widget naviBtn(
    BuildContext context, {
    String? image,
    Function? onClick,
    bool? disabled = false,
  }) {
    return FFInk(
      disabled: disabled,
      child: FFImageButton(
        alignment: Alignment.center,
        width: FFNaviHeight,
        heigth: FFNaviHeight,
        padding: AppNavi.padding,
        image: AssetImage(image!),
        onClick: onClick,
        // color: Colors.red,
      ),
    );
  }

  ///导航栏 返回按扭
  static Widget backBtn(BuildContext context, {String? image}) {
    return FFInk(
      child: FFImageButton(
        alignment: Alignment.center,
        width: FFNaviHeight,
        heigth: FFNaviHeight,
        padding: AppNavi.padding,
        image: AssetImage(image ?? AppNavi.naviBackImg),
        onClick: () {
          FFGoto.pop(context);
        },
        // color: Colors.red,
      ),
    );
  }

  /// title
  static FFGestureDetector titleWidget(BuildContext context, {String? title,Color? fontColor}) {
    return FFTextButton(
      notes: "导航栏title",
      text: title,
      heigth: FFNaviHeight,
      // color: Colors.red,
      fontSize: 22,
      fontColor: fontColor ?? rgba(226, 227, 221, 1),
      fontLineHeight: 28 / 22.0,
      margin: EdgeInsets.only(left: 6),
      // color: Colors.red,
    );
  }
}

enum DueNaviActionType {
  img,
  text,
  custom,
}

class DueNaviAction {
  DueNaviActionType type;
  String? value;
  FFGestureDetector? customWidget;
  Color? fontColor;
  bool? canClick;
  Function()? onClick;

  DueNaviAction({
    required this.type,
    this.canClick = true,
    this.value,
    this.onClick,
    this.customWidget,
    this.fontColor = Colors.black,
  });
}
